//
//  DealPhotosVC.m
//  DocumentWatermarking
//
//  Created by apple on 2018/9/20.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "DealPhotosVC.h"
#import "FQQCropImageView.h"
#import "WatermarkContentViewController.h"


@interface DealPhotosVC ()

@property (nonatomic) FQQCropImageView *cropImageView;

@property (nonatomic,strong)UIView * bottomView;
@property (nonatomic,strong)UIView * bottomCropView;

@property (nonatomic , assign) float rotationAngle;//旋转度数
@property (nonatomic , assign) float prerotationAngle;//旋转度数

@property (nonatomic,strong)NSMutableArray * cropImageArr;//储存裁剪后的图片

@property (nonatomic,strong)UIButton * rightButton;//右边的按钮

@property (nonatomic,assign)BOOL isFirstPhoto;//是否是第一张图片，默认yes

@property (nonatomic,strong)UIImage * firstImage;//第一张图片
@property (nonatomic,strong)UIImage * secondImage;//第二张图片


@end

@implementation DealPhotosVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _cropImageView = [[FQQCropImageView alloc]initWithImage:_imageArr[0]];
    _cropImageView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight-74);
    [self.view addSubview:_cropImageView];
    [self.view sendSubviewToBack:_cropImageView];
  
    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightButton.frame = CGRectMake(kScreenWidth - 80, 10, 80, 20);
    _rightButton.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, -20);
    [_rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _rightButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [_rightButton addTarget:self action:@selector(nextBtn) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:_rightButton];
    
    if (_isSelectTwo) {
        [_rightButton setTitle:@"下一步" forState:UIControlStateNormal];
    }else{
        [_rightButton setTitle:@"确定" forState:UIControlStateNormal];
    }
    
    _isFirstPhoto = YES;
    
    if (_isSelectTwo) {
        _firstImage = _imageArr[0];
        _secondImage = _imageArr[1];
        
    }else{
        _firstImage = _imageArr[0];
    }
    
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight - 74-64, kScreenWidth, 74)];
    _bottomView.backgroundColor = [UIColor colorWithHexString:@"333333"];
    [self.view addSubview:_bottomView];
    
    _bottomCropView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight - 74-64, kScreenWidth, 74)];
    _bottomCropView.backgroundColor = [UIColor colorWithHexString:@"333333"];
    [self.view addSubview:_bottomCropView];
    _bottomCropView.hidden = YES;
    
    //裁剪 翻转 旋转
    NSArray * image = @[@"裁切",@"矢量智能对象",@"矢量智能对象拷贝"];
    NSArray * title = @[@"裁切",@"垂直翻转",@"旋转90度"];
    for (int i =0; i<image.count; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*kScreenWidth/3, 0, kScreenWidth/3, 74);
        [btn setTitle:title[i] forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor clearColor];
        [btn setImage:[UIImage imageNamed:image[i]] forState:UIControlStateNormal];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(15, kScreenWidth/6-15, 30, 0)];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(44, 0, 0, 0)];
        [_bottomView addSubview:btn];
        
        btn.tag = 100+i;
        [btn addTarget:self action:@selector(cropAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    //裁剪view
    NSArray * cropBtnImage = @[@"取消",@"还原",@"确定"];
    NSArray * cropBtnTitle = @[@"取消",@"还原",@"确定"];
    for (int i =0; i<cropBtnImage.count; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*kScreenWidth/3, 0, kScreenWidth/3, 74);
        [btn setTitle:cropBtnTitle[i] forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor clearColor];
        [btn setImage:[UIImage imageNamed:cropBtnImage[i]] forState:UIControlStateNormal];
        [btn setImageEdgeInsets:UIEdgeInsetsMake(15, kScreenWidth/6-15, 30, 0)];
        [btn setTitleEdgeInsets:UIEdgeInsetsMake(44, 0, 0, 0)];
        [_bottomCropView addSubview:btn];
        
        btn.tag = 103+i;
        [btn addTarget:self action:@selector(cropAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
}

-(void)cropAction:(UIButton*)sender{
    NSInteger tag = sender.tag;
    switch (tag) {
            //裁剪
        case 100:
        {
            _bottomCropView.hidden = NO;
            _bottomView.hidden = YES;
        }
            break;
            //翻转
        case 101:
        {
            sender.selected = !sender.selected;
            if (sender.selected) {
                //获得初始transform
                CGAffineTransform transform = CGAffineTransformIdentity;
                //进行镜像变换(x轴左右反转)
                transform = CGAffineTransformScale(transform, -1, 1);
                _cropImageView.imageView.transform = transform;

            }else{
                //获得初始transform
                CGAffineTransform transform = CGAffineTransformIdentity;
                //进行镜像变换(x轴左右反转)
                transform = CGAffineTransformScale(transform, 1, 1);
                _cropImageView.imageView.transform = transform;
            }
        }
            break;
            //旋转90度
        case 102:
        {
            //转换成弧度
            CGFloat rotation = change2Radians(90);
            _cropImageView.imageView.transform = CGAffineTransformRotate(_cropImageView.imageView.transform, rotation);
        }
            break;
            //取消
        case 103:
        {
            [UIView animateWithDuration:0.2 animations:^{
                _cropImageView.imageView.frame = _cropImageView.imageViewOriginFrame;
                _cropImageView.cropView.frame = _cropImageView.imageViewOriginFrame;
                [_cropImageView layoutCoverViews];
                [_cropImageView layoutDecoraterViews];
                [_cropImageView setTouchRects];
            }];
            _bottomCropView.hidden = YES;
            _bottomView.hidden = NO;
        }
            break;
            //还原
        case 104:
        {
            [UIView animateWithDuration:0.2 animations:^{
                _cropImageView.imageView.frame = _cropImageView.imageViewOriginFrame;
                _cropImageView.cropView.frame = _cropImageView.imageViewOriginFrame;
                [_cropImageView layoutCoverViews];
                [_cropImageView layoutDecoraterViews];
                [_cropImageView setTouchRects];
            }];
        }
            break;
            //确定
        case 105:
        {
            UIImage *image = [_cropImageView cropImage];
            
            if (_isSelectTwo) {
                if (_isFirstPhoto) {
                    _firstImage = image;
                    [_cropImageView setImageViewWithImage:_firstImage];
//                    _cropImageView.imageView.image = _firstImage;
                }else{
                    _secondImage = image;
                    [_cropImageView setImageViewWithImage:_secondImage];
//                    _cropImageView.imageView.image = _secondImage;
                }
            }else{
                _firstImage = image;
                [_cropImageView setImageViewWithImage:_firstImage];
//                _cropImageView.imageView.image = _firstImage;
            }
            
            [UIView animateWithDuration:0.2 animations:^{
//                _cropImageView.imageView.frame = _cropImageView.imageViewOriginFrame;
                _cropImageView.cropView.frame = _cropImageView.imageView.frame;
                [_cropImageView layoutCoverViews];
                [_cropImageView layoutDecoraterViews];
                [_cropImageView setTouchRects];
            }];
            
            _cropImageView.contentMode = UIViewContentModeScaleAspectFit;
            _bottomCropView.hidden = YES;
            _bottomView.hidden = NO;
            
            
//            UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
//            imageView.center = self.view.center;
//            [self.view addSubview:imageView];
//
//            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTap:)];
//            tap.numberOfTouchesRequired = 1;
//            imageView.userInteractionEnabled = YES;
//            [imageView addGestureRecognizer:tap];
        }
            break;
            
        default:
            break;
    }
}

-(void)nextBtn{
    //两张图片
    if (_isSelectTwo) {
        //是否是第一张
        if (_isFirstPhoto) {
            _isFirstPhoto = NO;
            [_rightButton setTitle:@"确定" forState:UIControlStateNormal];
            [_cropImageView setImageViewWithImage:_imageArr[1]];
        }else{
            UIImage *finallImage = [Tools addImage:_firstImage toImage:_secondImage andSpace:5];
            WatermarkContentViewController *vc = [[WatermarkContentViewController alloc]init];
            vc.headerImage = finallImage;
            [self.navigationController pushViewController:vc animated:true];
        }
    }
    //一张图片
    else{
        WatermarkContentViewController * vc = [[WatermarkContentViewController alloc]init];
        vc.headerImage = _firstImage;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

//90度旋转
-(void)rotationWithNinety{
    
}

//刷新
- (void)reflash:(UIButton*)sender {
    
}
//done
- (void)done:(UIButton*)sender {
    
}

- (void)handleTap:(UIGestureRecognizer *)recognizer{
    [recognizer.view removeFromSuperview];
}

@end
